package hot100;

public class Lc114 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    private TreeNode pre = null;

    public void flatten(TreeNode root) {
        dfs(root);
    }

    void dfs(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.right != null) {
            dfs(root.right);
        }
        if (root.left != null) {
            dfs(root.left);
        }
        if (pre == null) {
            pre = root;
            return;
        }
        root.right = pre;
        pre = root;
        root.left = null;
    }
}
